package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dao.CategoryDao;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.dto.category.CategoryAddDTO;
import com.itheima.reggie.entity.dto.category.CategoryUpdateDTO;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import com.itheima.reggie.web.exception.BusinessException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * <p>
 * 菜品及套餐分类 服务实现类
 * </p>
 *
 * @author WuQiang
 * @since 2022-08-13
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    @Override
    public Page page(Long pageNum, Long pageSize) {
        // TODO: 2022/8/13  参数校验
        if (ObjectUtils.isEmpty(pageNum) || ObjectUtils.isEmpty(pageSize)) {
            throw new BusinessException("参数非法");
        }
        if (pageNum <= 0) {
            pageNum = 1L;
        }
        if (pageSize <= 0) {
            pageSize = 10L;
        }
        //2、核心业务
        Page<Category> pageByCondition = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Category> lqw = Wrappers.lambdaQuery(Category.class);
        //按照时间倒序排序
        lqw.orderByAsc(Category::getSort)
                .orderByDesc(Category::getUpdateTime)
                //投影查询
                .select(Category::getId, Category::getName, Category::getType, Category::getUpdateTime, Category::getSort);
        this.page(pageByCondition, lqw);
        return pageByCondition;
    }

    @Override
    public Boolean saveCategory(CategoryAddDTO dto) {
        // TODO: 2022/8/13 参数校验

        //2、核心业务
        //分类名称唯一性校验
        int count = this.count(Wrappers.lambdaQuery(Category.class).eq(Category::getName, dto.getName()));
        if (count > 0) {
            throw new BusinessException("分类名已存在，请换一个重试！");
        }
        //新增分类业务
        Category category = new Category();
        BeanUtils.copyProperties(dto, category);
        return this.save(category);
    }

    @Override
    public Boolean updateCategory(CategoryUpdateDTO dto) {
        // TODO: 2022/8/13 参数校验

        //2、核心业务
        //分类名唯一性校验
        List<Category> categoryList = this.list(Wrappers.lambdaQuery(Category.class)
                .eq(Category::getName, dto.getName()));
        if (categoryList != null || categoryList.size() > 0) {
            //排除掉查到自己的情况
            for (Category category : categoryList) {
                if (!category.getId().equals(dto.getId())) {
                    throw new BusinessException("分类名已经存在，请换一个");
                }
            }
        }
        //修改业务
        Category category = new Category();
        BeanUtils.copyProperties(dto, category);
        return this.updateById(category);
    }

    @Override
    public Boolean delCategory(Long id) {
        //1、参数校验
        if (ObjectUtils.isEmpty(id)) {
            throw new BusinessException("参数非法");
        }

        //2、核心业务
        //如果当前分类下关联菜品或者关联套餐，则抛出异常不能删除
        //查询菜品
        int dCount = dishService.count(Wrappers.lambdaQuery(Dish.class)
                .eq(Dish::getCategoryId, id));
        if (dCount > 0) {
            throw new BusinessException("当前分类下关联菜品，不能删除该分类！");
        }

        //查询套餐
        int sCount = setmealService.count(Wrappers.lambdaQuery(Setmeal.class)
                .eq(Setmeal::getCategoryId, id));
        if (sCount > 0) {
            throw new BusinessException("当前分类下关联套餐，不能删除该分类！");
        }

        //删除分类业务
        return this.removeById(id);
    }

    @Override
    public List<Category> getList(Integer type) {
        //核心业务
        List<Category> categories = this.list(Wrappers.lambdaQuery(Category.class)
                .eq(!ObjectUtils.isEmpty(type), Category::getType, type));
        return categories;
    }


}
